<html lang="en-US">

<head>
    
<title>Go1.18 新特性：编译后的二进制文件，将包含更多信息 - 花落雨忧</title>

<meta property="og:title" content="Go1.18 新特性：编译后的二进制文件，将包含更多信息 - 花落雨忧">



    



    
    <meta property="description" content="大家好，我是煎鱼。
我有一个朋友，，开开心心入职，想着施展拳脚，第一个任务就是对老旧的二进制文件进行研究。
他一看，这文件，不知道是编译器用什么参数怎么打出来的，环境不知道是什么，更不知道来自什么代码分支？
这除了是项目流程上的问题外，Go 在这块也有类似的小问题，处理起来比较麻烦。
[&amp;hellip;] 日常中很难从 Go 二进制文件中检索元信息，要么是信息完全缺失，要么提取需要对二进制文件进行 &amp;hellip;">
    <meta property="og:description" content="大家好，我是煎鱼。
我有一个朋友，，开开心心入职，想着施展拳脚，第一个任务就是对老旧的二进制文件进行研究。
他一看，这文件，不知道是编译器用什么参数怎么打出来的，环境不知道是什么，更不知道来自什么代码分支？
这除了是项目流程上的问题外，Go 在这块也有类似的小问题，处理起来比较麻烦。
[&amp;hellip;] 日常中很难从 Go 二进制文件中检索元信息，要么是信息完全缺失，要么提取需要对二进制文件进行 &amp;hellip;">
    






<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1">

<link rel="shortcut icon" href="https://www.lican.asia/logo/logo.png" type="image/x-icon" />



<link rel="stylesheet" href="/css/style.min.css" />

<link rel="stylesheet" href="/css/reset.min.css" />




<script src="https://www.lican.asia/js/highlight.min.js"></script>

<script>
hljs.configure({ ignoreUnescapedHTML: true })
hljs.highlightAll();
</script>


<script src="https://www.lican.asia/js/jquery.min.js"></script>




<link href="https://www.lican.asia/css/hugo-code.min.css" rel="stylesheet" />



    <style>
        .post-content img {
            max-width: 400px;
        }
    </style>
</head>

<body id="period" class="home blog">
    <a class="skip-content" href="#main">Press "Enter" to skip to content</a>
    <div id="overflow-container" class="overflow-container">
        <header class="site-header" id="site-header" role="banner">
    <div class="max-width">
        <div id="title-container" class="title-container">
            <div id="site-title" class="site-title"><a href="/">花落雨忧</a></div>
            <p class="tagline">思所及 力所行 方为真.</p>
        </div>
        
        <div id="menu-primary-container" class="menu-primary-container">
            <div class="icon-container">
            </div>
            <div id="menu-primary" class="menu-container menu-primary" role="navigation">
                <nav class="menu">
                    <ul id="menu-primary-items" class="menu-primary-items">
                        
                        
                        <li id="menu-item-0"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/posts" aria-current="page" tabindex="0">首页</a></li>
                        
                        <li id="menu-item-1"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/tech/" aria-current="page" tabindex="1">技术文档</a></li>
                        
                        <li id="menu-item-2"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/article/" aria-current="page" tabindex="2">文章</a></li>
                        
                        <li id="menu-item-3"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/project/" aria-current="page" tabindex="3">项目</a></li>
                        
                        <li id="menu-item-4"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/about/" aria-current="page" tabindex="4">关于</a></li>
                        
                    </ul>
                </nav>
            </div>
        </div>
    </div>
</header>

        <div id="primary-container" class="primary-container">
            <div class="max-width">
                <section id="main" class="main" role="main">
                    <div id="loop-container" class="loop-container">
                        <div
                            class="post type-post status-publish format-standard hentry entry">
                            <article>
                                <div class="post-container">
                                    <div class="post-header">
                                        <h2 class="post-title">
                                            <a href="/posts/posts/go/118-build-info/">Go1.18 新特性：编译后的二进制文件，将包含更多信息</a>
                                        </h2>
                                        
                                        <div class="post-byline">Published on
                                            <a class="date" href="javascript:;">2022/02/05</a>
                                            
                                            
                                            
                                            
                                            
                                        
                                    </div>
                                    <div class="post-content">
                                        <p>大家好，我是煎鱼。</p>
<p>我有一个朋友，，开开心心入职，想着施展拳脚，第一个任务就是对老旧的二进制文件进行研究。</p>
<p>他一看，这文件，不知道是编译器用什么参数怎么打出来的，环境不知道是什么，更不知道来自什么代码分支？</p>
<p>这除了是项目流程上的问题外，Go 在这块也有类似的小问题，处理起来比较麻烦。</p>
<h2 id="背景">背景</h2>
<p>日常中很难从 Go 二进制文件中检索元信息，要么是信息完全缺失，要么提取需要对二进制文件进行大量解析。</p>
<p>包含的元信息如下：</p>
<table>
<thead>
<tr>
<th>元信息</th>
<th>提取处</th>
</tr>
</thead>
<tbody>
<tr>
<td>Go 构建版本</td>
<td>符号表，通过全局变量 <code>runtime.buildVersion</code> 来获取</td>
</tr>
<tr>
<td>构建信息，例如：模块和版本</td>
<td>符号表，通过全局变量 <code>runtime/debug.modinfo</code> 来获取</td>
</tr>
<tr>
<td>编译器选项，例如：构建模式、编译器、gcflags、ldflags 等</td>
<td>无法获取</td>
</tr>
<tr>
<td>用户定义的自定义数据，例如：应用程序版本等</td>
<td>需在编译时设置全局字符串变量，才可以获取</td>
</tr>
</tbody>
</table>
<p>关注到编译器选项，也就是参数等都是无法得知的，也就是会提高获取如何编译出来的难度。</p>
<h2 id="新提案">新提案</h2>
<p>Michael Obermüller 提出了一个新的提案《<a href="https://github.com/golang/go/issues/35667">cmd/go: add compiler flags, relevant env vars to &lsquo;go version -m&rsquo; output</a>》用于解决上述问题。</p>
<p>在提案中想要的是 JSON 格式的结构输出：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;version&#34;</span>: <span style="color:#e6db74">&#34;go1.13.4&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;compileropts&#34;</span>: {
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;compiler&#34;</span>: <span style="color:#e6db74">&#34;gc&#34;</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;mode&#34;</span>: <span style="color:#e6db74">&#34;pie&#34;</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;os&#34;</span>: <span style="color:#e6db74">&#34;linux&#34;</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#960050;background-color:#1e0010">...</span>
</span></span><span style="display:flex;"><span>    },
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;buildinfo&#34;</span>: {
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;path&#34;</span>: <span style="color:#e6db74">&#34;脑子进煎鱼了&#34;</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;main&#34;</span>: {
</span></span><span style="display:flex;"><span>            <span style="color:#f92672">&#34;path&#34;</span>: <span style="color:#e6db74">&#34;HelloWorld&#34;</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#f92672">&#34;version&#34;</span>: <span style="color:#e6db74">&#34;(devel)&#34;</span>,
</span></span><span style="display:flex;"><span>        },
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;deps&#34;</span>: []
</span></span><span style="display:flex;"><span>    },
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;user&#34;</span>: {
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;customkey&#34;</span>: <span style="color:#e6db74">&#34;customval&#34;</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#960050;background-color:#1e0010">...</span>
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>Russ Cox 表示由于编译信息已有既有格式，并且默认使用 JSON 只会让二进制文件变得更大。好处少，没必要，改为了选项化的支持。</p>
<p>新的 Go1.18 版本中，可以通过既有的：</p>
<pre tabindex="0"><code>go version -m
</code></pre><p>查看到提案所提到的信息。</p>
<p>例如：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">$</span> <span style="color:#a6e22e">gotip</span> <span style="color:#a6e22e">version</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">go</span> <span style="color:#a6e22e">version</span> <span style="color:#a6e22e">devel</span> <span style="color:#a6e22e">go1</span><span style="color:#ae81ff">.18</span><span style="color:#f92672">-</span><span style="color:#a6e22e">eba0e866fa</span> <span style="color:#a6e22e">Mon</span> <span style="color:#a6e22e">Oct</span> <span style="color:#ae81ff">18</span> <span style="color:#ae81ff">22</span>:<span style="color:#ae81ff">56</span>:<span style="color:#ae81ff">07</span> <span style="color:#ae81ff">2021</span> <span style="color:#f92672">+</span><span style="color:#ae81ff">0000</span> <span style="color:#a6e22e">darwin</span><span style="color:#f92672">/</span><span style="color:#a6e22e">amd64</span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">$</span> <span style="color:#a6e22e">gotip</span> <span style="color:#a6e22e">build</span> .<span style="color:#f92672">/</span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">$</span> <span style="color:#a6e22e">gotip</span> <span style="color:#a6e22e">version</span> <span style="color:#f92672">-</span><span style="color:#a6e22e">m</span> <span style="color:#a6e22e">ko</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">build</span>	<span style="color:#a6e22e">compiler</span>	<span style="color:#a6e22e">gc</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">build</span>	<span style="color:#a6e22e">tags</span>	<span style="color:#a6e22e">goexperiment</span>.<span style="color:#a6e22e">regabiwrappers</span>,<span style="color:#a6e22e">goexperiment</span>.<span style="color:#a6e22e">regabireflect</span>,<span style="color:#a6e22e">goexperiment</span>.<span style="color:#a6e22e">regabiargs</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">build</span>	<span style="color:#a6e22e">CGO_ENABLED</span>	<span style="color:#66d9ef">true</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">build</span>	<span style="color:#a6e22e">CGO_CPPFLAGS</span>	
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">build</span>	<span style="color:#a6e22e">CGO_CFLAGS</span>	
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">build</span>	<span style="color:#a6e22e">CGO_CXXFLAGS</span>	
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">build</span>	<span style="color:#a6e22e">CGO_LDFLAGS</span>	
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">build</span>	<span style="color:#a6e22e">gitrevision</span>	<span style="color:#ae81ff">6447264</span><span style="color:#a6e22e">ff8b5d48aff64000f81bb0847aefc7bac</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">build</span>	<span style="color:#a6e22e">gituncommitted</span>	<span style="color:#66d9ef">true</span>
</span></span></code></pre></div><p>若需要输出 JSON 格式，也可以通过指定 <code>go version -json</code> 达到一样的效果。</p>
<p>在上面的输出中，现有的编译器选项等都会包含在内，能够让大家对整体编译后的二进制文件溯源有一个更好的认知。</p>
<h2 id="总结">总结</h2>
<p>在今天这篇文章中，给大家介绍了 Go1.18 的一个新的变化。</p>
<p>新版本中，编译器选项/参数、相关环境变量等，将会包含在编译后的二进制文件中，能够更便于后人排查和查看信息。</p>


                                        
                                        
                                        
                                        <div class="rp4wp-related-posts">
                                            <h3>相关文章</h3>
                                            <ul>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.asia/posts/posts/go/118-module/">Go1.18 新特性：多 Module 工作区模式</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.asia/posts/posts/go/var/">为什么 Go 有两种声明变量的方式，有什么区别，哪种好？</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.asia/posts/posts/go/map-con/">Go 为什么不在语言层面支持 map 并发？</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.asia/posts/posts/go/generics-design/">Go 泛型的 3 个核心设计，你学会了吗？</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.asia/posts/posts/go/nil-func/">Go 读者提问：值为 nil 也能调用函数，太神奇了吧？</a>
                                                    </div>
                                                </li>
                                                
                                            </ul>
                                        </div>
                                        
                                        
                                    </div>

                                    
                                    
                                    

                                    
                                    <div class="post-meta">
                                        
                                        
                                        <div class="post-tags">
                                            <ul>
                                            
                                            <li>
                                                <a href="/tags/go" title="View all posts tagged match">go</a>
                                            </li>
                                            
                                            </ul>
                                        </div>
                                        
                                        
                                        <nav class="further-reading">
                                            
                                            <div class="previous">
                                                <span>&lt;&lt; Prev</span>
                                                <a href="https://www.lican.asia/posts/posts/go/118-module/"
                                                    rel="prev">Go1.18 新特性：多 Module 工作区模式</a> </div>
                                            
                                            
                                            <div class="next">
                                                <span>Next >></span>
                                                <a href="https://www.lican.asia/posts/posts/go/118-cut/">Go1.18 新特性：新增好用的 Cut 方法</a> 
                                            </div>
                                            
                                        </nav>
                                    </div>
                                    
                                    

                                    
                                    
                                    

                                </div>
                            </article>
                        </div>
                    </div>
                </section>
                <aside class="sidebar sidebar-primary" id="sidebar-primary" role="complementary">
    <h1 class="screen-reader-text">Sidebar</h1>
    

    
    
    <section id="text-2" class="widget widget_text">
        <div class="textwidget">
            
            <div id="profile">
                <div id="profile_picture"><img src="https://www.lican.asia/logo/logo.png"></div>
                <div id="profile_intro">
                    <p><span class="name">Lican</span></p>
                    <p class="intro">全栈开发者，爱好造轮子。</p>
                </div>
            </div>
            
            <p>
                <script type="text/javascript">
                    (function ($) {
                        $(document).ready(function () {
                            var menuPrimaryContainer = $('#menu-primary-container');
                            var profile = $('#text-2');
                            $('#toggle-navigation').click(function () {
                                if (menuPrimaryContainer.hasClass('open')) {
                                    profile.removeClass('open');
                                } else {
                                    profile.addClass('open');
                                }
                            });
                        });
                    })(jQuery);
                </script>
            </p>
        </div>
    </section>
    
    
    
    
    
    <section id="text-5" class="widget widget_text">
        <h2 class="widget-title">开源项目</h2>
        <div class="textwidget">
            <div id="projects" style="line-height: 22px;">
                
                <a href="https://github.com/idoubi/gonews"
                    target="_blank">gonews</a>: &nbsp;Daily news for golang<br>
                
                <a href="https://github.com/idoubi/sql2struct"
                    target="_blank">sql2struct</a>: &nbsp;Generate go struct according to SQL<br>
                
                <a href="https://github.com/idoubi/goz"
                    target="_blank">goz</a>: &nbsp;Request library used in golang<br>
                
        </div>
    </section>
    
    

    
    
    
    
    <section id="qrcode" class="widget widget_media_image">
        <h2 class="widget-title">微信公众号</h2>
        <img width="258" height="258"
            src="https://www.lican.asia/wechat/lican.png"
            class="image wp-image-5514  attachment-full size-full" alt=""
            style="max-width: 100%; height: auto;"
            sizes="(max-width: 258px) 100vw, 258px">
    </section>
    
    

    
    
    

    
    
    

    
    
    
    
</aside>
            </div>
        </div>

        <footer id="site-footer" class="site-footer" role="contentinfo">
    <div class="max-width">
    </div>
    <div class="footer">
        <div id="footercontent">
            © lican.asia All rights reserved<br/>
            Built with Hugo Theme <a href="https://github.com/idoubi/hugo-theme-period" target="_blank">Period</a>
        </div>
    </div>
</footer>

<script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?e8351b6d4626d5881d439ea1f6184baa";
      var s = document.getElementsByTagName("script")[0]; 
      s.parentNode.insertBefore(hm, s);
    })();
</script>
    
    
    </div>
    
</body>

</html>